package com.ai.service.impl;

import com.ai.entity.SysLogEntity;
import com.ai.mapper.SysLogMapper;
import com.ai.service.LogService;
import com.ai.utils.ConvertUtils;
import com.ai.web.PageResp;
import com.ai.web.request.LogPageReq;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 日志管理
 */
@Service
public class LogServiceImpl implements LogService {

    @Resource
    SysLogMapper sysLogMapper;

    @Override
    public void deleteByIds(List<String> ids) {
        sysLogMapper.delete(Wrappers.<SysLogEntity>lambdaQuery().in(SysLogEntity::getId, ids));
    }

    @Override
    public PageResp<SysLogEntity> page(LogPageReq logPageReq) {
        LambdaQueryWrapper<SysLogEntity> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(logPageReq.getUsername())) {
            queryWrapper.like(SysLogEntity::getUsername, logPageReq.getUsername());
        }
        if (!StringUtils.isEmpty(logPageReq.getOperation())) {
            queryWrapper.like(SysLogEntity::getOperation, logPageReq.getOperation());
        }
        if (!StringUtils.isEmpty(logPageReq.getStartTime())) {
            queryWrapper.gt(SysLogEntity::getCreateTime, logPageReq.getStartTime());
        }
        if (!StringUtils.isEmpty(logPageReq.getEndTime())) {
            queryWrapper.lt(SysLogEntity::getCreateTime, logPageReq.getEndTime());
        }
        queryWrapper.orderByDesc(SysLogEntity::getCreateTime);
        IPage<SysLogEntity> iPage = sysLogMapper.selectPage(new Page<>(logPageReq.getPage(), logPageReq.getLimit()), queryWrapper);
        return ConvertUtils.convertPageResp(iPage);
    }

}
